package com.njworkorder.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.njworkorder.DTO.InsuranceRegistrationAddDto;
import com.njworkorder.Entity.InsuranceRegistration;
import com.njworkorder.Service.InsuranceRegistrationService;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import com.njworkorder.VO.InsuranceRegistrationVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@Tag(name = "保险登记管理",description = "保险登记管理相关接口")
@RestController
@RequestMapping("/insure")
public class InsureRegistrationController {

    private final InsuranceRegistrationService insuranceRegistrationService;
    public InsureRegistrationController(InsuranceRegistrationService insuranceRegistrationService) {
        this.insuranceRegistrationService = insuranceRegistrationService;
    }

    @Transactional
    @Operation(summary = "添加保险信息")
    @PostMapping("/addInfo")
    public Result<String> addInfo(@RequestBody List<InsuranceRegistrationAddDto> insuranceRegistrationAddDto,
                                  @RequestHeader("Create-Ap-Id") String createApId) {

        List<InsuranceRegistration> list = new ArrayList<>();
        InsuranceRegistration insuranceRegistration;
        for(InsuranceRegistrationAddDto item : insuranceRegistrationAddDto){
            insuranceRegistration = new InsuranceRegistration();
            BeanUtils.copyProperties(item, insuranceRegistration);
            insuranceRegistration.setCreateApId(createApId);
            list.add(insuranceRegistration);
        }

        return insuranceRegistrationService.saveBatch(list) ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败！");
    }

    @Operation(summary = "根据id删除保险信息")
    @GetMapping("/deleteById")
    public Result<String> deleteInfoById(@RequestParam("id") String id) {
        return insuranceRegistrationService.removeById(id) ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败！") ;
    }

    @Operation(summary = "根据id更新保险登记信息")
    @PostMapping("/updateById/{id}")
    public Result<String> updateById(@RequestBody InsuranceRegistrationAddDto insuranceRegistrationAddDto,@PathVariable("id") String id) {

        InsuranceRegistration insuranceRegistration = new InsuranceRegistration();
        BeanUtils.copyProperties(insuranceRegistrationAddDto, insuranceRegistration);
        insuranceRegistration.setId(id);

        return insuranceRegistrationService.updateById(insuranceRegistration) ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败！") ;
    }

    @Operation(summary = "根据id查询保险信息")
    @GetMapping("/getInfoById")
    public Result<InsuranceRegistrationVo> getInfoById(@RequestParam("id") String id) {

        MPJLambdaWrapper<InsuranceRegistration> wrapper = insuranceRegistrationService.getMPJWrapper()
                .eq(InsuranceRegistration::getId, id);

        InsuranceRegistrationVo insuranceRegistrationVo = insuranceRegistrationService.selectJoinOne(InsuranceRegistrationVo.class, wrapper);
        return ResultUtil.success(insuranceRegistrationVo);
    }

    @Operation(summary = "保险信息列表")
    @GetMapping("/getList")
    public Result<Page<InsuranceRegistrationVo>> getList(@RequestParam("pageIndex") int pageIndex,
                                                         @RequestParam("pageSize") int pageSize) {

        MPJLambdaWrapper<InsuranceRegistration> wrapper = insuranceRegistrationService.getMPJWrapper();

        Page<InsuranceRegistrationVo> insuranceRegistrationVoPage = insuranceRegistrationService.selectJoinListPage(new Page<>(pageIndex, pageSize), InsuranceRegistrationVo.class, wrapper);

        return ResultUtil.success(insuranceRegistrationVoPage);
    }


}
